//
//  MororcadeViewController.m
//  ICrEAM
//
//  Created by mac on 16/10/31.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MororcadeViewController.h"
#import "MororcadePresentController.h"

#import "EXTScope.h"
#import "MorocadeHeadView.h"
#import "MororcadeTableCell.h"
#import "MororcadeAction.h"
#import "MororcadeRespose.h"
@interface MororcadeViewController (){
    NSString *_searchStr;//切换的搜索条件
    BOOL _isOrder; //是否进行维修类型排序
    NSInteger _page;
    NSInteger _prePage;
}

//车间进度看板数组
@property(nonatomic,strong)NSMutableArray *mororcadeArr;
//车间进度弹出的控制器
@property(nonatomic,strong)UIPopoverPresentationController *popCtr;
//切换类型条件数组
@property(nonatomic,strong)NSArray *dataArr;;

@end

@implementation MororcadeViewController
-(NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr=@[@"",@"THREE ",@"FIVE",@"ACCIDENT",@"ALL"];
    }
    return _dataArr;
}
-(NSMutableArray *)mororcadeArr{
    if (!_mororcadeArr) {
        _mororcadeArr=[NSMutableArray array];
    }
   
    return _mororcadeArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.view.backgroundColor=[UIColor whiteColor];
    
    self.title=@"车间进度看板";
    [self setupNav];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshNotice) name:MororcadeNotice object:nil];
    
    // Do any additional setup after loading the view from its nib.
}
#pragma mark 执行通知的方法
-(void)refreshNotice{
    [self.tableView.mj_header beginRefreshing];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark ui布局
-(void)setupNav{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    view.backgroundColor=RGB(230, 236, 236);
    self.searchTextField.frame=CGRectMake(ScreenWidth-380, 4, 340, 36);
    self.searchTextField.placeholder=@"车牌号、送修人姓名、送修人手机、服务顾问";
    [view addSubview:self.searchTextField];
    self.switchArray=@[@"当天",@"三天内",@"5天内",@"事故车",@"全部"];
    self.mySwitch.frame=CGRectMake(0,0,ScreenWidth-380,44);
    [view addSubview:self.mySwitch];
    [self.view addSubview:view];
    [ self.mySwitch selectIndex:0 animated:YES];
    @weakify(self);
    [ self.mySwitch setWillBePressedHandler:^(NSUInteger index) {
        @strongify(self);
        _searchStr=self.dataArr[index];
        
        [self.tableView.mj_header beginRefreshing];
        
    }];
    //添加tableview的标题栏
    MorocadeHeadView *head=[MorocadeHeadView createMorocadeHeadView];
    head.isorder=_isOrder;
    head.repairTypeBlock=^(){
        @strongify(self);
        _isOrder=YES;
        [self.tableView.mj_header beginRefreshing];
    };
    head.translatesAutoresizingMaskIntoConstraints=NO;
    UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(view.frame), ScreenWidth, 48)];
    [view1 addSubview:head];
    [head mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(view1.mas_leading).with.offset(0);
        make.top.equalTo(view1.mas_top).with.offset(1);
        make.bottom.equalTo(view1.mas_bottom).with.offset(1);
        make.trailing.equalTo(view1.mas_trailing).with.offset(0);
    }];
    UIView *linew=[[UIView alloc]initWithFrame:CGRectMake(0, 47, ScreenWidth, 1)];
    linew.backgroundColor=RGB(204, 204, 204);
    [view1 addSubview:linew];
    UIView *linew1=[[UIView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, 1)];
    linew1.backgroundColor=RGB(204, 204, 204);
    [view1 addSubview:linew1];
    [self.view addSubview:view1];
    self.tableView.frame=CGRectMake(0, CGRectGetMaxY(view1.frame), ScreenWidth, ScreenHeight-158);
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _prePage=_page;
        _page=1;
        [self requestDataIsHead:YES];
    }];
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _prePage=_page;
        _page++;
        [self requestDataIsHead:NO];
    }];
    [self.tableView.mj_header beginRefreshing];
    
}
#pragma  mark 搜索框delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.tableView.mj_header beginRefreshing];
    [textField resignFirstResponder];
    return YES;
}

#pragma mark  获取车间进度看板数据
-(void)requestDataIsHead:(BOOL)ishead{
    MororcadeAction *act=[[MororcadeAction alloc]init];
    act.page=[NSString stringWithFormat:@"%d",_page];
    act.query_date=_searchStr;
    if (!IsStrEmpty(self.searchTextField.text)) {
        act.license=self.searchTextField.text;
    }
    if (_isOrder) {
        act.order=@"repairTypeCode";
    }
    [self requestDataWithAction:act haveHud:NO showError:NO success:^(__kindof YTKBaseRequest *request) {
        NSDictionary *resDic=request.responseJSONObject;
        NSArray *arr=resDic[@"TT_REPAIR_ORDER"];
        if (ishead) {
            [self.mororcadeArr removeAllObjects];
        }
        
        if ([arr isKindOfClass:[NSArray class]]) {
            if (!IsArrEmpty(arr)) {
                for (NSDictionary *dic in arr) {
                    MororcadeRespose *model=[MororcadeRespose mj_objectWithKeyValues:dic];
                    [self.mororcadeArr addObject:model];
                }
            }else{
                [self showText:@"没有更多结果"];
            }
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            [self showText:@"没有更多结果"];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(__kindof YTKBaseRequest *request) {
        
        _page=_prePage;
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 90;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.mororcadeArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MororcadeTableCell *cell=[MororcadeTableCell createMororcadeTableCell];
    if (indexPath.row==0) {
        UIView *topline=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        topline.backgroundColor=RGB(240, 240, 240);
        [cell.contentView addSubview:topline];
    }
    if (indexPath.row==self.mororcadeArr.count-1) {
        UIView *linew=[[UIView alloc]initWithFrame:CGRectMake(0, 89, ScreenWidth, 1)];
        linew.backgroundColor=RGB(240, 240, 240);
        [cell.contentView addSubview:linew];
    }
    MororcadeRespose *model=self.mororcadeArr[indexPath.row];
    cell.mororcadeModel=model;
    if (indexPath.row%2!=0) {
        cell.backgroundColor=RGB(247, 247, 247);
    }
    @weakify(self);
    cell.morocadeBlcok=^(RepairType type,MororcadeRespose *mororcadeModel){
        @strongify(self);
        MororcadePresentController *presentCtr=[[MororcadePresentController alloc]init];
        presentCtr.isOperation=mororcadeModel.isOperation;
        presentCtr.repairType=type;
        presentCtr.ro_num=mororcadeModel.proxyNum;
        presentCtr.modalPresentationStyle=UIModalPresentationPopover;
        UIPopoverPresentationController *pop=presentCtr.popoverPresentationController;
        pop.sourceRect=CGRectMake(0, 0,ScreenWidth, 1);
        pop.sourceView=self.view;
        [self presentViewController:presentCtr animated:YES completion:nil];
    };
    return cell;
}

@end
